/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.service.product;

import java.util.Arrays;
import java.util.List;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.modules.hk.shop.dao.product.HkShopProductSkuDao;
import com.thinkgem.jeesite.modules.hk.shop.entity.category.HkShopCategoryColor;
import com.thinkgem.jeesite.modules.hk.shop.entity.category.HkShopCategoryProperty;
import com.thinkgem.jeesite.modules.hk.shop.entity.category.HkShopCategorySpec;
import com.thinkgem.jeesite.modules.hk.shop.entity.product.*;
import com.thinkgem.jeesite.modules.hk.shop.entity.property.HkShopPropertyValue;
import com.thinkgem.jeesite.modules.hk.shop.service.category.HkShopCategoryColorService;
import com.thinkgem.jeesite.modules.hk.shop.service.category.HkShopCategoryPropertyService;
import com.thinkgem.jeesite.modules.hk.shop.service.category.HkShopCategorySpecService;
import com.thinkgem.jeesite.modules.hk.shop.service.property.HkShopPropertyService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.hk.shop.dao.product.HkShopProductDao;

/**
 * 乐购商品Service
 *
 * @author 罗天文
 * @version 2018-05-16
 */
@Service
@Transactional(readOnly = true)
public class HkShopProductService extends CrudService<HkShopProductDao, HkShopProduct> {
    @Autowired
    private HkShopProductPropertyService hkShopProductPropertyService;
    @Autowired
    private HkShopPropertyService hkShopPropertyService;
    @Autowired
    private HkShopProductImagesService hkShopProductImagesService;

    @Autowired
    private HkShopProductSkuService hkShopProductSkuService;


    @Autowired
    private HkShopProductSkuImagesService hkShopProductSkuImagesService;

    @Autowired
    private HkShopCategoryColorService hkShopCategoryColorService;


    @Autowired
    private HkShopCategorySpecService hkShopCategorySpecService;
    @Autowired
    private HkShopProductColorService hkShopProductColorService;


    @Autowired
    private HkShopProductSpecService hkShopProductSpecService;
    @Autowired
    private HkShopCategoryPropertyService hkShopCategoryPropertyService;
    @Autowired
    private HkShopProductSkuDao hkShopProductSkuDao;
    public HkShopProduct get(String id) {

        HkShopProduct p = super.get(id);
        HkShopCategoryProperty hkShopCategoryProperty = new HkShopCategoryProperty();
        hkShopCategoryProperty.setCategory(p.getCategory());

      /*  List<HkShopCategoryProperty> list = hkShopCategoryPropertyService.findList(hkShopCategoryProperty);
        for (HkShopCategoryProperty pp : list
        ) {
            pp.setProperty(hkShopPropertyService.get(pp.getProperty().getId()));
        }

        p.setHkShopCategoryPropertyList(list);*/


        HkShopProductImages hkShopProductImages = new HkShopProductImages();
        hkShopProductImages.setProduct(p);

        List<HkShopProductImages> img = hkShopProductImagesService.findList(hkShopProductImages);

        p.setHkShopProductImagesList(img);
        p.setHkShopProductSkuList(hkShopProductSkuDao.findList(new HkShopProductSku(p)));
/*
        HkShopCategoryColor hkShopCategoryColor = new HkShopCategoryColor();
        hkShopCategoryColor.setCategory(p.getCategory());

        List<HkShopCategoryColor> categorycolors = hkShopCategoryColorService.findList(hkShopCategoryColor);

        p.setCategorycolorsList(categorycolors);


        HkShopCategorySpec hkShopCategorySpec = new HkShopCategorySpec();
        hkShopCategorySpec.setCategory(p.getCategory());

        List<HkShopCategorySpec> categorySpecs = hkShopCategorySpecService.findList(hkShopCategorySpec);

        p.setCategoryspecsList(categorySpecs);


        HkShopProductColor hkShopProductColor = new HkShopProductColor();
        hkShopProductColor.setProduct(p);

        List<HkShopProductColor> colorsList = hkShopProductColorService.findList(hkShopProductColor);

        p.setColorsList(colorsList);


        HkShopProductSpec hkShopProductSpec = new HkShopProductSpec();
        hkShopProductSpec.setProduct(p);

        List<HkShopProductSpec> hkShopProductSpecs = hkShopProductSpecService.findList(hkShopProductSpec);

        p.setSpecsList(hkShopProductSpecs);*/





/*

        HkShopProductSku hkShopProductSku=new HkShopProductSku();
        hkShopProductSku.setProduct(p);
        List<HkShopProductSku> skus= hkShopProductSkuService.findList(hkShopProductSku);
        HkShopProductSkuImages hkShopProductSkuImages=new HkShopProductSkuImages();
        for (HkShopProductSku sku:skus
                ) {
             hkShopProductSkuImages.setSku(sku);
             sku.setSkuImagesList(hkShopProductSkuImagesService.findList(hkShopProductSkuImages));
        }
        p.setSkusList(skus);*/


        return p;
    }

    public List<HkShopProduct> findList(HkShopProduct hkShopProduct) {
        return super.findList(hkShopProduct);
    }

    public Page<HkShopProduct> findPage(Page<HkShopProduct> page, HkShopProduct hkShopProduct) {
        return super.findPage(page, hkShopProduct);
    }

    @Transactional(readOnly = false)
    public void save(HkShopProduct hkShopProduct) {
        if (hkShopProduct.getDescript() != null) {
            hkShopProduct.setDescript(StringEscapeUtils.unescapeHtml4(
                    hkShopProduct.getDescript()));
        }
       /* hkShopProduct.setImgSrc(hkShopProduct.getHkShopProductImagesList().get(0).getImgSrc());*/
        super.save(hkShopProduct);
      /*  HkShopProductProperty hkShopProductProperty2 = new HkShopProductProperty();
        hkShopProductProperty2.setProduct(hkShopProduct);
        hkShopProductPropertyService.deleteByProduct(hkShopProductProperty2);
        for (HkShopProductProperty hkShopProductProperty : hkShopProduct.getHkShopProductPropertyList()
        ) {
            hkShopProductProperty.setProduct(hkShopProduct);
            if (null != hkShopProductProperty.getPropertyvalue() && hkShopProductProperty.getPropertyvalue().length > 0) {
                hkShopProductProperty.setPropertyvalues(StringUtils.join(hkShopProductProperty.getPropertyvalue(), ','));
            }

            hkShopProductPropertyService.save(hkShopProductProperty);
        }*/
        for (HkShopProductImages hkShopProductImages : hkShopProduct.getHkShopProductImagesList()
        ) {
            hkShopProductImages.setIsmain("1");
            hkShopProductImages.setProduct(hkShopProduct);
            if (HkShopProductImages.DEL_FLAG_NORMAL.equals(hkShopProductImages.getDelFlag())) {
                if(StringUtils.isNotEmpty(hkShopProductImages.getImgSrc())) {
                    hkShopProductImagesService.save(hkShopProductImages);
                }
                else {
                    hkShopProductImagesService.delete(hkShopProductImages);
                }
            } else {
                hkShopProductImagesService.delete(hkShopProductImages);
            }

        }





        for (HkShopProductSku hkShopProductSku : hkShopProduct.getHkShopProductSkuList()){
            if (hkShopProductSku.getId() == null){
                continue;
            }
            if (HkShopProductSku.DEL_FLAG_NORMAL.equals(hkShopProductSku.getDelFlag())){



                if (StringUtils.isBlank(hkShopProductSku.getId())){
                    hkShopProductSku.setProduct(hkShopProduct);
                    hkShopProductSku.preInsert();
                    hkShopProductSkuDao.insert(hkShopProductSku);
                }else{
                    hkShopProductSku.preUpdate();
                    hkShopProductSkuDao.update(hkShopProductSku);
                }




            }else{
                hkShopProductSkuDao.delete(hkShopProductSku);
            }
        }
/*

        List<String> colorsList = Lists.newArrayList();
        for (HkShopProductColor hpc : colorsLists
        ) {
            colorsList.add(hpc.getCategoryColor().getId());
        }
        HkShopProductSpec hkShopProductSpec = new HkShopProductSpec();
        hkShopProductSpec.setProduct(hkShopProduct);

        List<HkShopProductSpec> hkShopProductSpecss = hkShopProductSpecService.findList(hkShopProductSpec);
        List<String> specList = Lists.newArrayList();
        for (HkShopProductSpec hpc : hkShopProductSpecss
        ) {
            specList.add(hpc.getCategorySpec().getId());
        }*/

      /*  //规格
        List<String> ss = Arrays.asList(hkShopProduct.getSpecs().split(","));
        specList.removeAll(ss);


        //颜色
        List<String> cs = Arrays.asList(hkShopProduct.getColors().split(","));
        colorsList.removeAll(cs);


        //删除sku配置

        hkShopProductColor = new HkShopProductColor();
        hkShopProductColor.setProduct(hkShopProduct);
        hkShopProductColorService.deleteByProduct(hkShopProductColor);

        hkShopProductSpec = new HkShopProductSpec();
        hkShopProductSpec.setProduct(hkShopProduct);
        hkShopProductSpecService.deleteByProduct(hkShopProductSpec);*/
/*
        String id = "";
        for (String c : colorsList
        ) {
            for (String s : specList
            ) {
                id = hkShopProductSkuService.getSku(hkShopProduct.getId(), c, s);
                if (com.thinkgem.jeesite.common.utils.StringUtils.isNotBlank(id)) {
                    hkShopProductSkuService.delete(new HkShopProductSku(id));

                }
            }
            hkShopProductSkuImagesService.deleteByProductColor(hkShopProduct, c);
        }*/

        /*List<HkShopProductColor> hkShopProductColors = Lists.newArrayList();
        HkShopCategoryColor hkShopCategoryColor;
        for (String c : cs
        ) {
            hkShopProductColor = new HkShopProductColor();
            hkShopProductColor.setProduct(hkShopProduct);
            hkShopCategoryColor = hkShopCategoryColorService.get(c);
            hkShopProductColor.setCategoryColor(hkShopCategoryColor);
            hkShopProductColor.setName(hkShopCategoryColor.getName());
            hkShopProductColorService.save(hkShopProductColor);
            hkShopProductColors.add(hkShopProductColor);


        }

        List<HkShopProductSpec> hkShopProductSpecs = Lists.newArrayList();
        HkShopCategorySpec hkShopCategorySpec;
        for (String s : ss
        ) {
            hkShopProductSpec = new HkShopProductSpec();
            hkShopProductSpec.setProduct(hkShopProduct);
            hkShopCategorySpec = hkShopCategorySpecService.get(s);
            hkShopProductSpec.setCategorySpec(hkShopCategorySpec);
            hkShopProductSpec.setName(hkShopCategorySpec.getName());
            hkShopProductSpecService.save(hkShopProductSpec);
            hkShopProductSpecs.add(hkShopProductSpec);

        }
        //增加sku配置
        for (HkShopProductColor c : hkShopProductColors
        ) {
            for (HkShopProductSpec s : hkShopProductSpecs
            ) {
                id = hkShopProductSkuService.getSku(hkShopProduct.getId(), c.getCategoryColor().getId(), s.getCategorySpec().getId());
                if (com.thinkgem.jeesite.common.utils.StringUtils.isBlank(id)) {
                    HkShopProductSku sku = new HkShopProductSku();
                    sku.setProduct(hkShopProduct);
                    sku.setProductColor(c);
                    sku.setProductSpec(s);
                    hkShopProductSkuService.save(sku);
                }
            }

        }*/

    }

    @Transactional(readOnly = false)
    public void delete(HkShopProduct hkShopProduct) {
        super.delete(hkShopProduct);
    }


    public HkShopProduct getPrice(String productId) {
        return dao.getPrice(productId);
    }


}